﻿import SiteMapManage.SiteMapM;
dynamic class SiteMapManage.SiteMapNode{
	private var $parent:SiteMapNode;
	private var $child:Array;
	private var $sitemap:SiteMapM;
	
	private var $nodeName:String;
	private var $nodeURL:String;
	
	private var $node:MovieClip;
	private var $nodeMc:MovieClip;
	private var $delMc:MovieClip;
	private var $editMc:MovieClip;
	private var $addMc:MovieClip;
	
	private var $siteDepthLevel:Number;
	private var $nodeId:Number;
	
	public var onRolloverNode:Function;  //onRolloverNode(node,enterTheNode:boolean);
	/////////////////////////////////////////////////////  
	public function get NodeId():Number{
		return this.$nodeId;
	}
	public function set NodeId(value:Number){
		this.$nodeId=value;
	}
	public function get Child():Array{
		return this.$child;
	}
	public function get SiteDepthLevel():Number{
		return this.$siteDepthLevel;
	}
	public function set SiteDepthLevel(value:Number){
		this.$siteDepthLevel=value;
	}
	public function get Parent():SiteMapNode{
		return this.$parent;
	}
	public function set Parent(value:SiteMapNode){
		this.$parent=value;
	}
	public function get NodeName():String{
		return this.$nodeName;
	}
	public function set NodeName(value:String){
		this.$nodeName=value;
		this.$nodeMc["txt"].text=value;
	}
	
	public function get NodeURL():String{
		return this.$nodeURL;
	}
	public function set NodeURL(value:String){
		this.$nodeURL=value;
	}
	public function get Node():MovieClip{
		return this.$node;
	}
	public function get NodeWidth():Number{
		return this.$nodeMc._width;
	}
	public function get NodeHeight():Number{
		return this.$nodeMc._height;
	}
	//////////////////////////////////////////////////////////////
	public function SiteMapNode(nodeName:String,smn:SiteMapM,depthLevel:Number){
		this.$sitemap=smn;
		this.$nodeName=nodeName;
		this.$nodeURL="";
		this.$siteDepthLevel=depthLevel;
		this.$child=new Array();
	}
	public function RemoveNode(){
		this.$node.removeMovieClip();
		Mouse.removeListener(this);
		for(var i=0;i<this.$child.length;i++){
			var cnode:SiteMapNode=SiteMapNode(this.$child[i]);
			if(cnode==undefined){
				
			}else{
				cnode.RemoveNode();
			}
		}
	}
	public function RemoveChildNode(nd:SiteMapNode){
		var index:Number=-1;
		for(var i=0;i<this.$child.length;i++){
			var cnode:SiteMapNode=SiteMapNode(this.$child[i]);
			if(cnode==nd){
				index=i;
				break;
			}
		}
		if(index<0){
			return;
		}
		this.$child[index].RemoveNode();
		var ar1:Array=this.$child.slice(index+1);
		this.$child=(this.$child.slice(0,index)).concat(ar1);
		this.$sitemap.DrawLine();
	}
	public function CreateNode(){
		
		var depth:Number=this.$sitemap.NodeLevel;
		this.$nodeId=depth;
		this.$node=this.$sitemap.RootMC.createEmptyMovieClip("node"+depth.toString(),depth);
		this.$nodeMc=this.$node.attachMovie(this.$sitemap.NodeLink,"node",9);
		this.$editMc=this.$node.attachMovie(this.$sitemap.EditLink,"edit",11);
		if(!this.$sitemap.ReadOnly){
			this.$addMc=this.$node.attachMovie(this.$sitemap.AddLink,"add",10);
			this.$delMc=this.$node.attachMovie(this.$sitemap.DelLink,"del",12);
		}
		this.NodeName=this.NodeName;
		onMouseMove();
		addEventOnMc();
		this.Say("created! depth:"+this.$siteDepthLevel);
	}
	
	public function getWidthWithChild():Number{
		if(this.$child.length<=0){
			return this.$nodeMc._width;
		}else{
			var w:Number=0;
			for(var i=0;i<this.$child.length;i++){
				var childn:SiteMapNode=SiteMapNode(this.$child[i]);
				w+=childn.getWidthWithChild()+this.$sitemap.NodeXOffset;
			}
			if(w>0) w-=this.$sitemap.NodeXOffset;
			return w;
		}
	}
	
	public function DrawLineToChild(){
		//Say(this.$nodeName+"----"+this.$child.length);
		if(this.$child.length>0){
			var dmc:MovieClip=this.$sitemap.SiteLineMc;
			this.$sitemap.SiteLineStyle();
			var point:Object={y:(this.Child[0].Node._y+this.Node._y)/2,x:this.Node._x};
			dmc.moveTo(this.Node._x,this.Node._y+this.NodeHeight/2);
			dmc.lineTo(point.x,point.y);
			for(var i=0;i<this.$child.length;i++){
				dmc.moveTo(point.x,point.y);
				dmc.lineTo(this.$child[i].Node._x,point.y);
				dmc.lineTo(this.$child[i].Node._x,this.$child[i].Node._y-this.$child[i].NodeHeight/2);
				this.$child[i].DrawLineToChild();
			}
		}
	}
	
	private function locationPS(){
		var h:Number=this.$sitemap.NodeBTNXOffset;
		var y:Number=this.$sitemap.NodeBTNYOffset;
		if(this.$addMc._visible){
			this.$addMc._x=h;
			this.$addMc._y=y;
			h+=this.$addMc._width;
			h+=this.$sitemap.NodeBTNPXL;
		}
		if(this.$editMc._visible){
			this.$editMc._x=h;
			this.$editMc._y=y;
			h+=this.$editMc._width;
			h+=this.$sitemap.NodeBTNPXL;
		}
		if(this.$delMc._visible){
			this.$delMc._x=h;
			this.$delMc._y=y;
			h+=this.$delMc._width;
			h+=this.$sitemap.NodeBTNPXL;
		}
	}
	
	
	
	private function setVisibleOfBTNs(f:Boolean){
		//trace("set editmc["+this.$editMc+"] = "+f)
		this.$addMc._visible=f;
		this.$editMc._visible=f;
		this.$delMc._visible=f;
		if(this.$parent==undefined){
			this.$delMc._visible=false;
		}
		this.locationPS();
	}
	private function addChildNode(){
		/*
		var nd:SiteMapNode=new SiteMapNode(this.root.$nodeName+"'s node",this.root.$sitemap,this.root.SiteDepthLevel+1);
		nd.Parent=this.root;
		nd.CreateNode();
		this.root.$child.push(nd);
		nd.$sitemap.LayoutSiteMap();
		nd.$sitemap.DrawLine();
		*/
		if(!this.root.$sitemap.BtnEnable) return;
		this.root.AddChildAndCreate("新节点","http://");
	}
	public function AddNode(node:SiteMapNode){
		node.Parent=this;
		this.$child.push(node);
	}
	public function AddChildAndCreate(name:String,url:String){
		
		var nd:SiteMapNode=new SiteMapNode(name,this.$sitemap,this.SiteDepthLevel+1);
		nd.Parent=this;
		nd.NodeURL=url;
		nd.CreateNode();
		this.$child.push(nd);
		nd.$sitemap.LayoutSiteMap();
		nd.$sitemap.DrawLine();
	}
	private function editChildNode(){
		//edit node information
		var nd:SiteMapNode=this.root;
		
		if(!nd.$sitemap.BtnEnable) return;
		
		var dp=nd.$sitemap.NodeLevel;
		var ep:MovieClip=nd.$sitemap.RootMC.attachMovie(nd.$sitemap.EditPanelLink,"editpanel"+dp,dp);
		//nd.Node._x;
		ep.node=nd;
		ep.sitemap=nd.$sitemap;
		ep.name.text=nd.NodeName;
		ep.url.text=nd.NodeURL;
		if(nd.$sitemap.ReadOnly){
			ep.ok._visible=false;
		}else{
			ep.ok.onRelease=function(){
				this._parent.node.NodeName=this._parent.name.text;
				this._parent.node.NodeURL=this._parent.url.text;
				this._parent.sitemap.BtnEnable=true;
				this._parent.removeMovieClip();
			}
		}
		ep.no.onRelease=function(){
			this._parent.sitemap.BtnEnable=true;
			this._parent.removeMovieClip();
		}
		ep.onEnterFrame=function(){
			this._x=this.node.Node._x-this._width/2;
			this._y=this.node.Node._y;
		}
		nd.$sitemap.BtnEnable=false;
	}
	private function delNode(){
		if(!this.root.$sitemap.BtnEnable) return;
		this.root.Parent.RemoveChildNode(this.root);
	}
	
	private function addBTNEvents(){
		
		if(!this.$sitemap.ReadOnly){
			this.$addMc.root=this;
			this.$delMc.root=this;
			this.$addMc.onRelease=addChildNode;
			this.$delMc.onRelease = delNode;
			this.$addMc.onRollOut = this.hidemouse;
			this.$addMc.onRollOver = this.showmouse;
			this.$delMc.onRollOut = this.hidemouse;
			this.$delMc.onRollOver = this.showmouse;
		}
		this.$editMc.root=this;
		this.$node.root=this;
		
			this.$editMc.onRollOut = this.hidemouse;
			this.$editMc.onRollOver = this.showmouse;
		this.$editMc.onRelease=editChildNode;
	}
	private function showmouse() {
		Mouse.show();
		_root.mousecur._visible = false;
	}
	private function hidemouse() {
		Mouse.hide();
		_root.mousecur._visible = true;
	}
	private function addEventOnMc(){
		Mouse.removeListener(this);
		Mouse.addListener(this);
		addBTNEvents();
	}
	private function onMouseMove(){
		if(this.$node.hitTest(_root._xmouse,_root._ymouse,true) && !this.$editMc._visible){
			setVisibleOfBTNs(true);
			onRolloverNode(this,true);
		}else if(!this.$node.hitTest(_root._xmouse,_root._ymouse,true) && this.$editMc._visible){
			setVisibleOfBTNs(false);
			onRolloverNode(this,false);
		}
	}
	
	private function onMouseDown(){
		if(this.$nodeMc.hitTest(_root._xmouse,_root._ymouse)){
			this.$node.startDrag();
		}
	}
	private function onMouseUp(){
		this.$node.stopDrag();
		if(this.$node.hitTest(_root._xmouse,_root._ymouse)){
			this.$sitemap.LayoutSiteMap(this);
			this.$sitemap.DrawLine();
		}
	}
	private function Say(o:Object){
		trace("SN["+this.$nodeName+"]: "+o.toString());
	}
}